package rulusoft.nook.rssReader;

import java.text.SimpleDateFormat;
import java.util.ArrayList;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ArrayAdapter;
import android.widget.Button;

public class rssItemDisplayer extends Activity {
    
	public static RssItem selectedRssItemOn = null;
	
	/** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
        
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, 
				WindowManager.LayoutParams.FLAG_FULLSCREEN);
                
        setContentView(R.layout.rss_item_displayer);
        
        nextbtn=(Button)findViewById(R.id.nextbtn);        
        
        //getting index from listview on reader activity
        Bundle listViewBundle=getIntent().getExtras();
        list_index=listViewBundle.getInt("list_index");
        Bundle urlBundle=getIntent().getExtras();
        urlfeed=urlBundle.getString("feed url");
        
        //list_index=0;
        
    	ArrayList<RssItem> newItems = RssItem.getRssItems(urlfeed);
        rssItems.clear();
        rssItems.addAll(newItems);
              
        previosbtn=(Button)findViewById(R.id.previosbtn);
        preferencebtn=(Button)findViewById(R.id.preferencesbtn);
        
        exitbtn=(Button)findViewById(R.id.exitbtn);
        exitbtn.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				finish();
				System.exit(0);
			}
		});
        
        homebtn=(Button)findViewById(R.id.homebtn);
        homebtn.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent intent=new Intent(v.getContext(), RssReaderActivity.class);
				startActivityForResult(intent, 0);
				finish();
			}
		});
        
        //enabling web view
        wv=(WebView)findViewById(R.id.webview_compontent);
        
        // workaround so that the default browser doesn't take over
        wv.setHorizontalScrollBarEnabled(false);
        wv.setVerticalScrollBarEnabled(false);
        wv.setWebViewClient(new MyWebViewClient());
        //web settings
        ws=wv.getSettings();
        //font size
        ws.setDefaultFontSize(20);
        
        //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
        //!!!!!!!!!!!!LOADING WEB VIEW CONTENT!!!!!!!!!!!!
        //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
        
		//loading rss Items
		
        
        selectedRssItem = rulusoft.nook.rssReader.RssReaderActivity.selectedRssItem;		
		selectedRssItem=rssItems.get(list_index);
        loadWebViewContent();
		
		//loading next content
		nextbtn.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				list_index++;
				loadWebViewContent();
				//wv.reload();
			}
		});
		previosbtn.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				if(list_index>0) {
				list_index--;
				loadWebViewContent();
				}
			}
		});
		
    }
    private class MyWebViewClient extends WebViewClient {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            view.loadUrl(url);
            return true;
        }
        @Override
        public void onPageFinished(WebView view, String url) {
        	rssItemDisplayer.this.setTitle(dtitle);
    		final String js = "javascript:(function () {var w = " + 6 
    				+ ";for( var i = 0; i < document.images.length; i++ ) {"
    				   +"var img = document.images[i];"
    				    +"if( img.width > w ) {"
    				      +"img.height = Math.round( img.height * ( w/img.width ) );"
    				      +"img.width = w;"
    				      +"img.style.display='block';"
    				    +"};"
    				  +"}"
    				+"''})();";
        	wv.loadUrl(js);
        }
    }
    //loading content
    public void loadWebViewContent() {
		selectedRssItem=rssItems.get(list_index);
    	//selectedRssItem = rulusoft.nook.rssReader.RssReaderActivity.selectedRssItem;		
    	String title = "";
    	SimpleDateFormat sdf = new SimpleDateFormat("MM/dd - hh:mm:ss");
    	title = "\n" + "<b>" + selectedRssItem.getTitle() + "</b>" + "   ( "
    			+ sdf.format(selectedRssItem.getPubDate()) + " )\n\n";
    	String content = "";
    	content += title + selectedRssItem.getDescription() + "\n"
    			+ selectedRssItem.getLink();
    			
    	//change activity window parametres
    	//displayerWindow=getWindow();
    	//displayerWindow.setTitle(title.toString());
    	        
    	//get page title
    	//dtitle = title;
    	        
    	wv.requestFocus();
    	wv.loadData("<?xml version='1.0' encoding='UTF-8' ?>"+content, "text/html", "utf-8");
    }
    //WebView objects        
    public WebView wv;
    public WebSettings ws;
    private Window displayerWindow;
    //page title
    private static String dtitle;
    //menu buttons
    private Button homebtn;
    private Button nextbtn;
    private Button previosbtn;
    private Button exitbtn;
    private Button preferencebtn;
    //selectedRssItem displaying on WebView    
    public static RssItem selectedRssItem = rulusoft.nook.rssReader.RssReaderActivity.selectedRssItem;
    public ArrayList<RssItem> rssItems = new ArrayList<RssItem>();
    ArrayAdapter<RssItem> aa = null;
    public String urlfeed;
    //listview index
    public static int list_index=0;
    
    //refresh rss Item list button
}
